package rest.rabarbar.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;



import rest.rabarbar.convert.ConvertJson;
import rest.rabarbar.model.BookBean;

@Controller
public class HomeController {

	private List<String> idList = null;

	@RequestMapping(value = "/")
	public String test(Model model) throws IOException {
	//	model.addAttribute("ksiazkaBean", new BookBean());
	//	model.addAttribute("mojaMapa", createExampleMap());
		model.addAttribute("ksiazkaBean", this.idList);
		
		return "home" ;
	}

	private Map<String, String> createExampleMap() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("1", "a");
		map.put("2", "b");
		map.put("3", "c");
		map.put("4", "d");
		return map;
	}
	
	@POST
	@Path("/send")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response consumeJSON( String str ) {

		String output = str + " we did it";

		return Response.status(200).entity(output).build();
	}

	
	@GET
	@Path ("/updatelist")
//	@Consumes(MediaType.APPLICATION_JSON)
	public void updateIdList(String str) {
		
		System.out.println("poszedl update " + str);
		this.idList = (List<String>) ConvertJson.convertFromJsonToList(str);
		// return Response.ok(new Viewable("/index.jsp", (List)
		// ConvertJson.convertFromJsonToList(str) ) ).build();
	}
}
